package com.sakura.mall.ware.purchase.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.ware.purchase.domain.entity.PurchaseEntity;
import com.sakura.mall.ware.purchase.domain.request.AddPurchaseRequest;
import com.sakura.mall.ware.purchase.domain.request.UpdatePurchaseRequest;
import com.sakura.mall.ware.purchase.domain.request.PurchasePageRequest;
import com.sakura.mall.ware.purchase.domain.vo.PurchaseVo;
import com.sakura.mall.ware.purchase.mapper.PurchaseMapper;
import com.sakura.mall.ware.purchase.service.PurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, PurchaseEntity> implements PurchaseService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddPurchaseRequest request) {
        // todo 其他校验逻辑
        PurchaseEntity purchase = request.convert(PurchaseEntity.class);
        this.save(purchase);
    }

    @Override
    public Page<PurchaseVo> queryPage(PurchasePageRequest request) {
        IPage<PurchaseEntity> qp = request.toPage();
        LambdaQueryWrapper<PurchaseEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<PurchaseVo> ret = qp.convert(entity -> entity.convert(PurchaseVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdatePurchaseRequest request) {
        PurchaseEntity entity = request.convert(PurchaseEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

